package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/oppty/report"
	cluePb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/clue"
	microGfyxUserService "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.myscrm.cn/golang/stark/v4"
)

type reportFollowRepository struct {
}

func NewReportFollowRepository() ReportFollowRepositoryIface {
	return &reportFollowRepository{}
}

func (r *reportFollowRepository) SaveFollowRecord(ctx context.Context, req *report.SaveFollowRecordRequest) (*cluePb.SaveFollowRecordResponse, error) {
	request := cluePb.SaveFollowRecordRequest{
		Data: &cluePb.FollowRecord{
			ReportId:      req.ReportId,
			FollowType:    req.FollowType,
			FollowStatus:  req.FollowStatus,
			FollowContent: req.FollowContent,
			Province:      req.Province,
			ProvinceCode:  req.ProvinceCode,
			City:          req.City,
			CityCode:      req.CityCode,
			Address:       req.Address,
			Images:        req.Images,
		},
	}
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := cluePb.NewReportServiceClient(conn)
	return client.SaveFollowRecord(ctx, &request)
}

func (r *reportFollowRepository) GetFollowRecordList(ctx context.Context, reportId string, page int32, PageSize int32) (*cluePb.GetFollowRecordListResponse, error) {
	request := &cluePb.GetFollowRecordListRequest{
		ReportId: reportId,
		Page:     page,
		PageSize: PageSize,
	}
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := cluePb.NewReportServiceClient(conn)
	return client.GetFollowRecordList(ctx, request)
}

func (r *reportFollowRepository) UpdateReportShare(ctx context.Context, userUid string, code string, data *microGfyxUserService.ReportFromSharing) (bool, error) {
	request := &microGfyxUserService.UpdateReportShareRequest{
		RecipientUid: userUid,
		Code:         code,
		Status:       data,
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return false, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxUserService.NewReportServiceClient(conn)
	_, err = client.UpdateReportShare(ctx, request)
	if err != nil {
		stark.Logger.Infof(ctx, "UpdateReportShare-error: %+v, request: %+v", request, err)
		return false, err
	}

	return true, nil
}
